package com.ymanage.controller;
import java.util.ArrayList;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ymanage.entity.Logistics;
import com.ymanage.service.LogisticsService;

@Controller
public class LogisticsController {
	
	@Autowired
	public LogisticsService logisticsService;
	
	List<Logistics> list =new ArrayList<Logistics>();
	
	Logistics logs = new Logistics();
	
	@RequestMapping("/logistics.do")
	public String selectLogistics(HttpServletRequest request,Logistics logistics){
		if(logistics!=null){
			list=logisticsService.selectAll(logistics);
			request.setAttribute("Logistics", list);
			return "logistics.jsp";
		}
		return "index.html";
	}
	
	@RequestMapping("/logisticsUpdate.do")
	public String updateLogistics(HttpServletRequest request,Integer lid){
		if(lid != null){
			logs.setLid(lid);
			list=logisticsService.selectAll(logs);
			if(list.size()>0){
				logs=list.get(0);
				request.setAttribute("Logistics", logs);
				request.getSession().setAttribute("lid", lid);
				return "logisticsUpasAdd.jsp";
			}
		}
		return "redirect:logistics.do";
	}
	
	@RequestMapping("/logisticsAddasUpdate.do")
	public String updateorAdd(Logistics logistics,HttpSession session){
		if(session.getAttribute("lid")!=null){
			logistics.setLid((Integer) session.getAttribute("lid"));
			session.removeAttribute("lid");
			logisticsService.update(logistics);
			return "redirect:logistics.do";
		}else{
			logisticsService.add(logistics);
			return "redirect:logistics.do";
		}
	}
	
	@RequestMapping("/logisticsDel.do")
	public String delLogistics(Logistics logistics){
		logisticsService.del(logistics);
		return "redirect:logistics.do";
	}
}
